﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BrandUnion.CustomData;

public partial class BookingSite_Booking : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        btnSubmit.Click += new EventHandler(btnSubmit_Click);
        btnChangeTime.Click += new EventHandler(btnChangeTime_Click);
        if (!IsPostBack)
        {
            DataBind();
        }
    }

    void btnChangeTime_Click(object sender, EventArgs e)
    {
        pnlConfirmation.Visible = false;
        pnlDetails.Visible = false;
        pnlSlots.Visible = true;
    }

    void btnSubmit_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            BrandUnion.CustomData.BookingFactory factory = new BrandUnion.CustomData.BookingFactory();
            BrandUnion.CustomData.Booking booking = factory.GetByEmailAddress(txtEmail.Text.ToString());
            if (booking != null)
            {
                pnlErrorMessage.Visible = true;
            }
            else
            {
                booking = new BrandUnion.CustomData.Booking();
                booking.BookingSlotId = SelectedId;
                booking.Address1 = txtAddress1.Text;
                booking.Address2 = txtAddress2.Text;
                booking.EmailAddress = txtEmail.Text;
                booking.FirstName = txtFirstName.Text;
                booking.LastName = txtSurname.Text;
                try
                {
                    booking.Age = int.Parse(txtAge.Text);
                }
                catch (FormatException)
                {
                }
                if (rdoSex.SelectedValue == "0")
                    booking.Gender = Acms.Bll.User.Genders.Male;
                else
                    booking.Gender = Acms.Bll.User.Genders.Female;
                booking.Tel = txtPhone.Text;
                booking.MoreInfo = !chkAgree.Checked;

                factory.Save(booking);
                string displayName = string.Concat(booking.FirstName, " ", booking.LastName).Trim();
                Acms.Bll.Email email = Acms.Bll.Email.CreateInstance(displayName, booking.EmailAddress, Server.MapPath("~/Emails/JeanGeanieEmail_Harlequin.xml"));
                email.Format("DisplayName", displayName);
                email.Format("AppointmentDate", booking.DateTime.ToLongDateString());
                email.Format("AppointmentTime", booking.DateTime.ToShortTimeString());
                email.Send();

                pnlDetails.Visible = false;
                pnlConfirmation.Visible = true;
            }
        }
    }

    private List<BrandUnion.CustomData.BookingSlot> _BookingSlots;
    private List<BrandUnion.CustomData.BookingSlot> BookingSlots
    {
        get
        {
            if (_BookingSlots == null)
            {
                _BookingSlots = GetBookingSlotFactory().GetAll();
            }
            return _BookingSlots;
        }
    }

    private BookingFactory _BookingFactory;
    protected BookingFactory GetBookingFactory()
    {
        if (_BookingFactory == null)
        {
            _BookingFactory = new BookingFactory();
        }
        return _BookingFactory;
    }

    private BookingSlotFactory _BookingSlotFactory;
    protected BookingSlotFactory GetBookingSlotFactory()
    {
        if (_BookingSlotFactory == null)
        {
            _BookingSlotFactory = new BookingSlotFactory();
        }
        return _BookingSlotFactory;
    }

    protected void listTimes_DataBinding(object sender, EventArgs e)
    {
        ListView listTimes = sender as ListView;
        var item = listTimes.NamingContainer as Acms.WebControls.SimpleRepeaterItem;
        if (item != null)
        {
            var date = (DateTime)item.DataItem;
            var data = BookingSlots.Where(x => x.DateTime.Date == date).ToList();

            listTimes.DataSource = data;
        }
    }

    protected void listTimes_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            BookingSlot slot = ((ListViewDataItem)e.Item).DataItem as BookingSlot;
            Panel pnlItem = e.Item.FindControl("pnlItem") as Panel;
            LinkButton btnItem = e.Item.FindControl("btnItem") as LinkButton;

            if (slot != null && pnlItem != null && btnItem != null)
            {
                if (slot.NoSlots <= slot.Booked || slot.DateTime < DateTime.Now)
                {
                    pnlItem.CssClass = "item full";
                    btnItem.Enabled = false;
                }
                else
                {
                    btnItem.Enabled = true;
                }
            }
        }
    }

    protected void listTimes_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        if (e.CommandName == "SelectDate")
        {
            _Selected = null;
            SelectedId = int.Parse((string)e.CommandArgument);
            if (Selected != null)
            {
                lblSelectedTime.Text = Selected.DateTime.ToString("ddd dd/MM/yyyy @ HH:mm");
                pnlSlots.Visible = false;
                pnlDetails.Visible = true;
            }
        }
    }

    private int SelectedId;
    private BookingSlot _Selected;
    private BookingSlot Selected
    {
        get
        {
            if (_Selected == null && SelectedId > 0)
            {
                _Selected = GetBookingSlotFactory().GetById(SelectedId);
            }
            return _Selected;
        }
    }

    protected override object SaveViewState()
    {
        ViewState["SelectedId"] = SelectedId;
        return base.SaveViewState();
    }

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        SelectedId = (int)ViewState["SelectedId"];
    }

    protected void dsDates_Select(object sender, Acms.DataControls.EventDrivenDataSourceSelectEventArgs e)
    {
        var data = BookingSlots.Select(x => x.DateTime.Date).Distinct().ToArray();
        e.SetData(data);
    }

}
